home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / ccmd / split.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  2.5 KB  |  83 lines

  1. /*
  2.  Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  the City of New York.  Permission is granted to any individual or
  4.  institution to use, copy, or redistribute this software so long as it
  5.  is not sold for profit, provided this copyright notice is retained.
  6.  
  7.  Author: Andrew Lowry
  8. */
  9. /* This file reads from standard input and distributes what it reads
  10. ** to output files as directed by the input stream.  Distribution is
  11. ** signaled by lines of the form:
  12. **
  13. **     #file xxx
  14. **
  15. ** When such a line is encountered, the current output file is closed
  16. ** and the file named xxx is opened and made the current output file.
  17. ** The line containing the "#file" marker is not output to any file.
  18. **/
  19.  
  20. #include <stdio.h>
  21.  
  22. /* miscellaneous definitions */
  23.  
  24. #define TRUE -1
  25. #define FALSE 0
  26.  
  27. #define NULCHAR '\000'
  28. #define TAB '\011'
  29. #define NEWLINE '\n'
  30. #define SPACE '\040'
  31.  
  32. #define BUFSIZE 100
  33.  
  34. char inbuf[BUFSIZE];
  35.  
  36. FILE *out = stdout;
  37.  
  38. main()
  39. {
  40.   while (fgets(inbuf, BUFSIZE, stdin) != NULL) /* read until EOF */
  41.     if (!chkfile())        /* check each line for #file */
  42.       fputs(inbuf,out);        /* write other lines */
  43.   fclose(out);            /* close up most recent output file */
  44. }
  45.  
  46. int
  47. chkfile()
  48. {
  49.   char *test = "#FILE";        /* this is what we look for */
  50.   char *fname;
  51.   char tc,ic;            /* chars from test and input strings */
  52.   int i;
  53.  
  54.   for (i = 0; (tc = test[i]) != NULCHAR; i++) { /* loop through test string */
  55.     ic = inbuf[i];        /* get next input char */
  56.     if ((ic >= 'a') && (ic <= 'z')) /* convert to upper case */
  57.       ic -= 'a'-'A';
  58.     if (tc != ic)        /* chars don't match? */
  59.       return(FALSE);        /* then not a file line */
  60.   }
  61.   if ((inbuf[i] != SPACE) && (inbuf[i] != TAB))
  62.     return(FALSE);        /* must be followed by whitespace */
  63.   else
  64.     i++;
  65.   while ((inbuf[i] == SPACE) || (inbuf[i] == TAB))
  66.     i++;            /* skip remaining whitespace */
  67.   fname = &inbuf[i];        /* point at beginning of filename */
  68.   while (inbuf[i] != NULCHAR)    /* search for and remove newline or space */
  69.     if ((inbuf[i] == NEWLINE) || (inbuf[i] == SPACE) || (inbuf[i] == TAB))
  70.       inbuf[i] = NULCHAR;    /* replace it with null char */
  71.     else
  72.       i++;
  73.   fprintf(stderr,"*** Opening %s for output\n",fname);
  74.   if (out != stdout)
  75.     fclose(out);        /* close previous output file */
  76.   out = fopen(fname,"w");    /* open up specified file */
  77.   if (out == NULL) {
  78.     fprintf(stderr,"*** Could not open %s, using standard out\n",fname);
  79.     out = stdout;        /* use stdout if open fails */
  80.   }
  81.   return(TRUE);            /* tell them not to output this line */
  82. }
  83.